﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vorkos_dev_snake
{
    [Serializable]
    public class Snake
    {
        public Field CurrentField { get; set; }
        private Queue<Coordinate> snakeCoordinates;
        private Movement lastMove;
        public void SetNewMove(Snake sender, Movement moveDirrection)
        {
            Coordinate[] flatSnake = this.snakeCoordinates.ToArray();
            Coordinate nextCoordinate = flatSnake[flatSnake.Length - 1];
            if (moveDirrection == Movement.None) moveDirrection = this.lastMove;
            else this.lastMove = moveDirrection;
            switch (moveDirrection)
            {
                case Movement.Up:
                    nextCoordinate.Y--;
                    break;
                case Movement.Down:
                    nextCoordinate.Y++;
                    break;
                case Movement.Left:
                    nextCoordinate.X--;
                    break;
                case Movement.Right:
                    nextCoordinate.X++;
                    break;
            }
            GameLogic.Instance.CheckCollision(this, nextCoordinate);
            this.snakeCoordinates.Enqueue(nextCoordinate);
            if (!GameLogic.Instance.CheckFood(this, nextCoordinate))
            {
                this.snakeCoordinates.Dequeue();
            }

            else
            {
                this.CurrentField.RemoveFood(nextCoordinate);
                GameLogic.Instance.IncreaseSpeed();
            }
        }
        public Snake(Field curField)
        {
            this.CurrentField = curField;
            Coordinate initPosition = new Coordinate();
            initPosition = GameLogic.Instance.UI.GetCenterPoint();
            this.lastMove = Movement.Left;
            this.snakeCoordinates = new Queue<Coordinate>();
            this.snakeCoordinates.Enqueue(initPosition);
            initPosition.X--;
            this.snakeCoordinates.Enqueue(initPosition);
            initPosition.X--;
            this.snakeCoordinates.Enqueue(initPosition);
            initPosition.X--;
            this.snakeCoordinates.Enqueue(initPosition);
        }

        public Queue<Coordinate> GetSnakeCoordinates()
        {
            return snakeCoordinates;
        }
    }
    [Serializable]
    public struct Coordinate
    {
        public ushort X { get; set; }
        public ushort Y { get; set; }
    }
}
